//
//  HYUserPhoto.m
//  HaiYue
//
//  Created by skyline on 2018/1/5.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYUserPhoto.h"
@interface HYUserPhoto()
@property (nonatomic, strong) HYImageView *photoIMG;
@property (nonatomic, strong) HYLabel *nameLBL;
@property (nonatomic, strong) HYLabel *vipLvLBL;
@property (nonatomic, strong) HYImageView *rightIMG;
@end
@implementation HYUserPhoto
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.bounds =CGRectMake(0, 0, ceil(self.photoIMG.bounds.size.width + self.nameLBL.bounds.size.width + self.rightIMG.bounds.size.width + GAP), HYPhotoWidth);
        self.vipLvLBL.hidden = NO;
    }
    return self;
}

- (HYImageView *)photoIMG
{
    if (!_photoIMG)
    {
        _photoIMG = [[HYImageView alloc]initWithImage:[UIImage imageNamed:@"default_gameicon"] cornerRadius:HYPhotoRadius];
        _photoIMG.layer.borderWidth = 2;
        _photoIMG.layer.borderColor = RGBA(130, 240, 240, 1).CGColor;
        [self addSubview:_photoIMG];
        [_photoIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(self.photoIMG.bounds.size);
        }];
    }
    return _photoIMG;
}

- (HYLabel *)nameLBL
{
    if (!_nameLBL)
    {
        _nameLBL = [[HYLabel alloc]initWithText:@"点击登录" textColor:[UIColor whiteColor]];
        [self addSubview:_nameLBL];
        [_nameLBL mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.photoIMG.mas_right).with.offset(GAP / 2.0);
            make.bottom.equalTo(self.mas_centerY);
        }];
    }
    return _nameLBL;
}
- (HYLabel *)vipLvLBL
{
    if (!_vipLvLBL)
    {
        _vipLvLBL = [[HYLabel alloc] initWithText:@"Lv:" textColor:[UIColor whiteColor] textFont:[UIFont systemFontOfSize:TipsFont weight:UIFontWeightLight]];
        [self addSubview:_vipLvLBL];
        [_vipLvLBL mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.photoIMG.mas_right).with.offset(GAP / 2.0);
            make.top.equalTo(self.mas_centerY).with.offset(GAP / 2.0);
        }];
    }
    return _vipLvLBL;
}
- (HYImageView *)rightIMG
{
    if (!_rightIMG)
    {
        _rightIMG = [[HYImageView alloc]initWithImage:[UIImage imageNamed:@"right"]];
        _rightIMG.bounds = CGRectMake(0, 0, HYCellRightArrowWidth, HYCellRightArrowHeight);
        [self addSubview:_rightIMG];
        [_rightIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLBL.mas_right).with.offset(GAP / 2.0);
            make.centerY.equalTo(self.nameLBL);
            make.size.mas_equalTo(self.rightIMG.bounds.size);
        }];
    }
    return _rightIMG;
}

- (void)setModel:(HYUserModel *)model
{
    if (_model != model)
    {
        _model = model;
        self.nameLBL.text = ([self.model.nickname length] > 1)?self.model.nickname:@"点击登录";
        self.vipLvLBL.text = [NSString stringWithFormat:@"Lv:%@",self.model.viplevel == nil?@"0":self.model.viplevel];
        self.photoIMG.image = ([self.model.photo length] > 1)?[UIImage imageWithURL:self.model.photo]:[UIImage imageNamed:@"default_gameicon"];
        self.bounds =CGRectMake(0, 0, ceil(self.photoIMG.bounds.size.width + self.nameLBL.bounds.size.width + self.rightIMG.bounds.size.width + GAP), HYPhotoWidth);
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(self.bounds.size);
        }];
    }
    
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
